<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title data-i18n="resources.title_findPath"></title>
    <style type="text/css">
        body {
            margin: 0;
            overflow: hidden;
            background: #fff;
            width: 100%;
            height: 100%
        }

        #map {
            position: absolute;
            width: 100%;
            height: 100%;
            border: 1px solid #3473b7;
        }

        #toolbar {
            position: absolute;
            top: 50px;
            right: 10px;
            text-align: center;
            z-index: 100;
            border-radius: 4px;
        }
    </style>
</head>
<body>
<div id="toolbar" class="panel panel-primary">
    <div class='panel-heading'>
        <h5 class='panel-title text-center' data-i18n="resources.text_findPath"></h5></div>
    <div class='panel-body content'>
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_sites" onclick="selectPoints()"/>&nbsp;
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_submit" onclick="findPath()"/>&nbsp;
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_clear" onclick="clearElements()"/>
    </div>
</div>
<div id="map"></div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
<script>
    var host = window.isLocal ? window.server : "http://support.supermap.com.cn:8090";
    var map, layer, vectorLayer, markerLayer,
        drawPoint, select,
        nodeArray = [], pathTime, pathListIndex = 0, routeCompsIndex = 0,
        style = {
            strokeColor: "#304DBE",
            strokeWidth: 3,
            pointerEvents: "visiblePainted",
            fill: false
        },
        styleGuidePoint = {
            pointRadius: 10,
            externalGraphic: "../img/walk.png"
        },
        styleGuideLine = {
            strokeColor: "#25FF25",
            strokeWidth: 6,
            fill: false
        },
        url1 = host + "/iserver/services/map-changchun/rest/maps/长春市区图",
        url2 = host + "/iserver/services/transportationanalyst-sample/rest/networkanalyst/RoadNet@Changchun";

    init();

    function init() {
        vectorLayer = new SuperMap.Layer.Vector("Vector Layer");
        drawPoint = new SuperMap.Control.DrawFeature(vectorLayer, SuperMap.Handler.Point);
        select = new SuperMap.Control.SelectFeature(vectorLayer, {
            onSelect: onFeatureSelect,
            onUnselect: onFeatureUnselect
        });
        drawPoint.events.on({"featureadded": drawCompleted});
        map = new SuperMap.Map("map", {
            controls: [
                new SuperMap.Control.Zoom(),
                new SuperMap.Control.Navigation({
                    dragPanOptions: {
                        enableKinetic: true
                    }
                }),
                drawPoint,
                select], units: "m"
        });
        map.addControl(new SuperMap.Control.LayerSwitcher(), new SuperMap.Pixel(42, 80));
        layer = new SuperMap.Layer.TiledDynamicRESTLayer("Changchun", url1, {
            transparent: true,
            cacheEnabled: true
        }, {maxResolution: "auto"});
        layer.events.on({"layerInitialized": addLayer});
        markerLayer = new SuperMap.Layer.Markers("Markers");
    }

    function addLayer() {
        map.addLayers([layer, vectorLayer, markerLayer]);
        map.setCenter(new SuperMap.LonLat(4503.6240321526, -3861.911472192499), 1);
    }

    function selectPoints() {
        widgets.alert.clearAlert();
        clearElements();
        drawPoint.activate();
    }

    function drawCompleted(drawGeometryArgs) {
        var point = drawGeometryArgs.feature.geometry,
            size = new SuperMap.Size(44, 33),
            offset = new SuperMap.Pixel(-(size.w / 2), -size.h),
            icon = new SuperMap.Icon("./images/marker.png", size, offset);
        markerLayer.addMarker(new SuperMap.Marker(new SuperMap.LonLat(point.x, point.y), icon));
        nodeArray.push(point);
    }

    //选中时显示路径指引信息
    function onFeatureSelect(feature) {
        if (feature.attributes.description) {
            popup = new SuperMap.Popup("chicken",
                feature.geometry.getBounds().getCenterLonLat(),
                new SuperMap.Size(200, 30),
                "<div style='font-size:.8em; opacity: 0.8'>" + feature.attributes.description + "</div>",
                null, false);
            feature.popup = popup;
            map.addPopup(popup);
        }
        if (feature.geometry.CLASS_NAME != "SuperMap.Geometry.Point") {
            feature.style = styleGuideLine;
            vectorLayer.redraw();
        }
    }

    //清除要素时调用此函数
    function onFeatureUnselect(feature) {
        map.removePopup(feature.popup);
        feature.popup.destroy();
        feature.popup = null;
        if (feature.geometry.CLASS_NAME != "SuperMap.Geometry.Point") {
            feature.style = style;
        }
        vectorLayer.redraw();

    }

    function findPath() {
        widgets.alert.clearAlert();
        drawPoint.deactivate();
        var findPathService, parameter, analystParameter, resultSetting;
        resultSetting = new SuperMap.REST.TransportationAnalystResultSetting({
            returnEdgeFeatures: true,
            returnEdgeGeometry: true,
            returnEdgeIDs: true,
            returnNodeFeatures: true,
            returnNodeGeometry: true,
            returnNodeIDs: true,
            returnPathGuides: true,
            returnRoutes: true
        });
        analystParameter = new SuperMap.REST.TransportationAnalystParameter({
            resultSetting: resultSetting,
            weightFieldName: "length"
        });
        parameter = new SuperMap.REST.FindPathParameters({
            isAnalyzeById: false,
            nodes: nodeArray,
            hasLeastEdgeCount: false,
            parameter: analystParameter
        });
        if (nodeArray.length <= 1) {
            widgets.alert.showAlert(resources.msg_findPath,false);
        }
        findPathService = new SuperMap.REST.FindPathService(url2, {
            eventListeners: {"processCompleted": processCompleted}
        });
        findPathService.processAsync(parameter);
    }

    function processCompleted(findPathEventArgs) {
        var result = findPathEventArgs.result;
        allScheme(result);
    }

    function allScheme(result) {
        if (pathListIndex < result.pathList.length) {
            addPath(result);
        } else {
            pathListIndex = 0;
            //线绘制完成后会绘制关于路径指引点的信息
            addPathGuideItems(result);
        }
    }

    //以动画效果显示分析结果
    function addPath(result) {
        if (routeCompsIndex < result.pathList[pathListIndex].route.components[0].components.length) {
            var pathFeature = new SuperMap.Feature.Vector();
            var points = [];
            for (var k = 0; k < 2; k++) {
                if (result.pathList[pathListIndex].route.components[0].components[routeCompsIndex + k]) {
                    points.push(new SuperMap.Geometry.Point(result.pathList[pathListIndex].route.components[0].components[routeCompsIndex + k].x, result.pathList[pathListIndex].route.components[0].components[routeCompsIndex + k].y));
                }
            }
            var curLine = new SuperMap.Geometry.LinearRing(points);
            pathFeature.geometry = curLine;
            pathFeature.style = style;
            vectorLayer.addFeatures(pathFeature);
            //每隔0.001毫秒加载一条弧段
            pathTime = setTimeout(function () {
                addPath(result);
            }, 0.001);
            routeCompsIndex++;
        } else {
            clearTimeout(pathTime);
            routeCompsIndex = 0;
            pathListIndex++;
            allScheme(result);
        }
    }

    function addPathGuideItems(result) {
        //vectorLayer.removeAllFeatures();
        //显示每个pathGuideItem和对应的描述信息
        for (var k = 0; k < result.pathList.length; k++) {
            var pathGuideItems = result.pathList[pathListIndex].pathGuideItems, len = pathGuideItems.length;
            for (var m = 0; m < len; m++) {
                if (pathGuideItems[m].geometry.CLASS_NAME !== "SuperMap.Geometry.Point") {
                    continue;
                }
                var guideFeature = new SuperMap.Feature.Vector();
                guideFeature.geometry = pathGuideItems[m].geometry;
                guideFeature.attributes = {description: pathGuideItems[m].description};
                guideFeature.style = styleGuidePoint;
                vectorLayer.addFeatures(guideFeature);
            }
        }
        select.activate();
    }

    function clearElements() {
        widgets.alert.clearAlert();
        pathListIndex = 0;
        routeCompsIndex = 0;
        nodeArray = [];
        select.deactivate();
        if (vectorLayer.selectedFeatures.length > 0) {
            map.removePopup(vectorLayer.selectedFeatures[0].popup);
        }
        vectorLayer.removeAllFeatures();
        markerLayer.clearMarkers();
    }
</script>

</body>
</html>